import {request} from "../../request/index.js"

const app = getApp()
Page({
  data: {
    checkCode: '',
    input: '',
    name: '',
    accountNumber: '',
    code: '',
    encryptedData: '',
    iv: '',
    password: '',
    userName: ''
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  //刚进入页面随机先获取一个
    this.createCode()

  },
  huanyizhang(){
    this.createCode()
  },

  createCode() {
    //首先默认code为空字符串
    var checkCode = '';
    //设置长度，这里看需求，我这里设置了4
    var codeLength = 4;
    //设置随机字符
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    //循环codeLength 我设置的4就是循环4次
    for (var i = 0; i < codeLength; i++) {
      //设置随机数范围,这设置为0 ~ 36
      var index = Math.floor(Math.random() * 36);
      //字符串拼接 将每次随机的字符 进行拼接
      checkCode += random[index];
    }

    //将拼接好的字符串赋值给展示的code
    this.setData({
      checkCode
    })
    // console.log(this.data.checkCode)
  },
  formCode:function(e){
    this.setData({
      name: e.detail.value
    })
    var checkCode = this.data.checkCode
    var name = this.data.name
    
    if(checkCode != name.toUpperCase()) {
      wx.showToast({
        title: '验证码输入错误，请重新输入',
        icon: 'none',
        mask: true
      })
      this.createCode()
    }
  },
  getInfo: function(e) { 
    console.log(e)
    var that = this
    that.setData({
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
    })

    wx.getSetting({
      //调用微信小程序中的权限弹框
      success: function(res) {
        wx.login({
          success(result) {
            that.setData({
              code: result.code
            })
            
            request({
              url: "/user/registerUser", 
              method: 'POST',
              data: {
                accountNumber: that.data.accountNumber,
                code: that.data.code,
                encryptedData: that.data.encryptedData,
                iv: that.data.iv,
                password: that.data.password,
                userName: that.data.userName
              }, 
              header: {
                'content-type': 'application/x-www-form-urlencoded' // post请求默认值
              }
            })
            .then(res => {
              if(res.data.status !== 500 && e.detail.hasOwnProperty('rawData')) {
                if(that.data.accountNumber && that.data.password && that.data.userName && that.data.checkCode) {
                  wx.navigateTo({
                    url: '../login/login',
                  })
                } else {
                  wx.showToast({
                    title: '请重新填写注册信息',
                    icon: 'none',
                    mask: true
                  })
                }
              } else if(res.data.status === 500) {
                wx.showToast({
                  title: '注册的用户信息重复，请重新注册',
                  icon: 'none',
                  mask: true
                })
              } else {
                wx.showToast({
                  title: '注册失败，请确认是否授权',
                  icon: 'none',
                  mask: true
                })
              }
            })
          }
        }) 
         let scope = res.authSetting["scope.address"];
         if(scope == false){
            //  打开设置页面
            wx.openSetting({
            //如果点击了开启按钮，那么会调用成功回调。
              success: function(res) {
                //  打开之后再次获取
                wx.chooseAddress({
                  success: function (res) {
                  }
                })
              }
            })
         }
      }
    })
  },

  formName(e) {
    var userName = e.detail.value
    this.setData({
      userName
    })
    if(userName.length > 20) {
      wx.showToast({
        title: '输入的用户名过长，请重新输入',
        icon: 'none',
        mask: true
      })
    }
  },
  formNumber(e) {
    var number = e.detail.value
    if(!/^[0-9]*$/g.test(number)) {
      wx.showToast({
        title: '户号只能输入数字，请重新输入',
        icon: 'none',
        mask: true
      })
    }
    this.setData({
      accountNumber: number
    })
  },
  getPassword(e) {
    this.setData({
      password: e.detail.value
    })
    if(e.detail.value.length < 4) {
      wx.showToast({
        title: '密码长度必须大于或等于4位',
        icon: 'none',
        mask: true
      })
    }
  }
})
